package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Dao.*;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Service.DictionariesService;
import com.realtyManage.springboot.Service.IHouseFansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Service
@Transactional
public class HouseFansServiceImpl implements IHouseFansService {
    @Autowired
    private HouseFansRepository houseFansRepository;

    @Autowired
    private BrowseVolumeRepository browseVolumeRepository;

    @Autowired
    private ExternalConnectionRepository externalConnectionRepository;

    @Autowired
    private SysUserJurisdictionRepository sysUserJurisdictionRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    protected BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public HouseFans save(HouseFans houseFans) {
        return houseFansRepository.saveAndFlush(houseFans);
    }

    @Override
    public String findFans(String thisTime) {
        return houseFansRepository.findFans(thisTime);
    }

    @Override
    public String countBrowseVolume() {
        return browseVolumeRepository.countBrowseVolume();
    }

    @Override
    public String countHouseNum() {
        return browseVolumeRepository.countHouseNum();
    }

    public Integer getNum(){
        return browseVolumeRepository.getNum();
    }

    public Integer addNum(){
        Integer num = browseVolumeRepository.getNum();
        ++num;
        return browseVolumeRepository.addNum(num);
    }

    public void insertImportNum(){
        Integer ImportNum = browseVolumeRepository.getImportNum();
        ++ImportNum;
        browseVolumeRepository.addImportNum(ImportNum);
    }

    public void insertDistributionNum(){
        Integer DistributionNum = browseVolumeRepository.getDistributionNum();
        ++DistributionNum;
        browseVolumeRepository.addDistributionNum(DistributionNum);
    }

    public void insertBeltCustomerNum(){
        Integer beltCustomerNum = browseVolumeRepository.getBeltCustomerNum();
        ++beltCustomerNum;
        browseVolumeRepository.addBeltCustomerNum(beltCustomerNum);
    }

    public void insertDealNum(){
        Integer DealNum = browseVolumeRepository.getDealNum();
        ++DealNum;
        browseVolumeRepository.addDealNum(DealNum);
    }

    public  List<ExternalConnection> getExternalConnectionList(){
        return externalConnectionRepository.findAll();
    }


    public Integer allocation(String userId,String jurisdictionId){
        Integer code = 0;
        List userIds = new ArrayList();
        String ids[] = userId.split(",");
        for (String id : ids){
            User u = new User();
            u.setJurisdiction(jurisdictionId);
            u.setId(Integer.valueOf(userId));
            if (userRepository.updateUserJurisdiction(u) > 0){
                code = 1;
            }
        }
        return code;
    }


    public Integer allocationAndInsertUser(String userUser,String phone,String jurisdictionId,String level){
        Integer code = 0;
            User u = new User();
            u.setJurisdiction(jurisdictionId);
            u.setName(userUser);
            u.setTelephone(phone);
            u.setLevel(Integer.valueOf(level));
            u.setJoinTime(new Date());
            u.setStatus(1);
            if (userRepository.userCount(phone) > 0){
                if (userRepository.updateUser(u) > 0){
                    code = 1;
                }
            }else{
                u.setPwd(bCryptPasswordEncoder.encode("123456"));
                if (userRepository.save(u).getId() > 0){
                    code = 1;
                }
            }
        return code;
    }



}
